package com.gxa.service.impl;

import com.gxa.pojo.GoodsStorage;
import com.gxa.pojo.InStorage;
import com.gxa.pojo.InStorageGoods;
import com.gxa.pojo.Merchandise;
import com.gxa.service.InStorageService;
import com.gxa.mapper.InStorageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.*;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date 2020/9/21 18:12
 */
@Service(value = "inStorageService")

public class InStorageServiceImpl implements InStorageService {

    @Resource
    InStorageMapper inStorageMapper;

    /**
     * 查询所有供应商名字
     * @return 供应商名字
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<Map<String,Object>>  findSupplierName() {
        return inStorageMapper.findSupplierName();
    }

    /**
     * 查询所有仓库名字
     * @return 供仓库名字
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<Map<String,Object>> findStoreName() {

        return  inStorageMapper.findStoreName();
    }
    /**
     * 根据时间或者供货厂商或者进货仓库或者审核状态查询
     * @param map
     * @return 入库信息
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<InStorage>  findInStorage(Map map) {
        return inStorageMapper.findInStorage(map);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer countFindInStorage(Map<String,Object> map) {
        return inStorageMapper.countFindInStorage(map);
    }

    /**
     *
     * @param page 起始页
     * @param limit 分页大小
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<InStorage> findAllInStorageByPage(Integer page, Integer limit) {
        return inStorageMapper.findInStorageByPage(page,limit);
    }

    /**
     *
     * @return 返回总数据数
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public Integer findAllInStorage() {
        return inStorageMapper.findAllInStorage();
    }

    /**
     *  修改审批状态
     * @param
     * @return 返回影响行数
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public Integer updateState(Map map) {
        Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
        map.put("inStorageTime",timestamp);
        Integer line = inStorageMapper.updateState(map);
        Integer id = (Integer) map.get("id");
        List<Map> inStorageGoods = inStorageMapper.findInStorageGoodss(id);
        for (Map inStorageGood : inStorageGoods) {
            String goods = (String) inStorageGood.get("goods");
            GoodsStorage goodsStorageByName = inStorageMapper.findGoodsStorageByName(goods);
            if(goodsStorageByName!=null){
                inStorageMapper.updateGoodsStorageStock((Double) inStorageGood.get("bQuantity"),(Double) inStorageGood.get("sQuantity"),goodsStorageByName.getId());
            }else{
                Merchandise merchandiseByName = inStorageMapper.findMerchandiseByName(goods);
                String brand = merchandiseByName.getBrand();
                String type = merchandiseByName.getType();
                inStorageGood.put("brand",brand);
                inStorageGood.put("classification",type);
                inStorageMapper.insertGoodsStorage(inStorageGood);
            }
        }

        return line;
    }

    /**
     *  通过id查询入库信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public InStorage findInStorageById(Integer id) {
        return inStorageMapper.findInStorageById(id);
    }

    /**
     *  通过入库id查询对应的商品信息
     * @param inStorageId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<Map<String, Object>> findInStorageGoodsByInStorageId(Integer inStorageId) {
        return inStorageMapper.findInStorageGoodsByInStorageId(inStorageId);
    }

    /**
     *  查询品牌名称
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<Map<String, Object>> findBrandName() {
        return inStorageMapper.findBrandName();
    }

    /**
     *  查询所有的分类名称
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<Map<String, Object>> findLevelName() {
        return inStorageMapper.findLevelName();
    }

    /**
     *  通过id删除入库信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public Integer deleteInStorageById(Integer id) {
        return inStorageMapper.deleteInStorageById(id);
    }

    /**
     *  通过入库id删除对应的入库商品信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public Integer deleteInStorageGoodsByInStorageId(Integer id) {
        return inStorageMapper.deleteInStorageGoodsByInStorageId(id);
    }

    /**
     * 插入入库信息
     * @param
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer insertInStorage(Map map) {
        Map instorage = (Map) map.get("instorage");
        Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
        instorage.put("createdDate",timestamp);
        Integer integer = inStorageMapper.insertInStorage(instorage);
        Integer inStorageId = inStorageMapper.findInStorageId();
        List<Map> goods = (List<Map>) map.get("instoragegoods");
        for(Map good:goods) {
            good.put("inStorageId",inStorageId);
            inStorageMapper.insertInStorageGoods(good);
//            String name = (String) good.get("goods");
//            GoodsStorage goodsStorageByName = inStorageMapper.findGoodsStorageByName(name);
//            if (goodsStorageByName!=null){
//                inStorageMapper.updateGoodsStorageStock((Integer) good.get("bQuantity"),(Integer) good.get("sQuantity"),goodsStorageByName.getId());
//            }else {
//                inStorageMapper.insertGoodsStorage(good);
//            }

        }
        return integer;
    }


    /**
     * 根据品牌、分类查询商品信息
     * @param map 品牌 分类
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String, Object>> findInStorageGoods(Map<String, Object> map) {
        return inStorageMapper.findInStorageGoods(map);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer countFindInStorageGoods(Map<String, Object> map) {
        return inStorageMapper.countFindInStorageGoods(map);
    }

    /**
     * 全查询
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String, Object>> findAllInStorages() {
        return inStorageMapper.findAllInStorages();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer countFindAllInStorages() {
        return inStorageMapper.countFindAllInStorages();
    }

    /**
     *  更新入库信息
     * @param map
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer updateInStorage(Map map) {
        Map instorage = (Map) map.get("instorage");
        int id = Integer.parseInt(instorage.get("id").toString());
        instorage.put("id",id);
        Integer integer = inStorageMapper.updateInStorage(instorage);

        List<Map> inStorageGoodss = inStorageMapper.findInStorageGoodss(id);
        for (Map storageGoodss : inStorageGoodss) {
            String goods = (String) storageGoodss.get("goods");
            GoodsStorage goodsStorageByName = inStorageMapper.findGoodsStorageByName(goods);
            if(goodsStorageByName!=null){
                inStorageMapper.updateGoodsStorageStock2((Double) storageGoodss.get("bQuantity"),(Double) storageGoodss.get("sQuantity"),goodsStorageByName.getId());
            }
        }
        Integer integer1 = inStorageMapper.deleteInStorageGoods(instorage);

        List<Map> goods = (List<Map>) map.get("instoragegoods");
        for (Map good : goods) {
            int instorageId = Integer.parseInt(good.get("instorageId").toString());
            good.put("instorageId",instorageId);
            inStorageMapper.insertInStorageGoodsByInStorageId(good);
            String goods1 = (String) good.get("goods");
            GoodsStorage goodsStorageByName = inStorageMapper.findGoodsStorageByName(goods1);
            if(goodsStorageByName!=null){
                inStorageMapper.updateGoodsStorageStock3((Integer) good.get("bQuantity"),(Integer) good.get("sQuantity"),goodsStorageByName.getId());
            }else{
                Merchandise merchandiseByName = inStorageMapper.findMerchandiseByName(goods1);
                String brand = merchandiseByName.getBrand();
                String type = merchandiseByName.getType();
                good.put("brand",brand);
                good.put("classification",type);
                inStorageMapper.insertGoodsStorage(good);
            }
        }
        return integer;
    }
}
